<template>
    <div class="flex justify-center items-center min-h-screen">
        <div class="bg-white p-8 rounded-lg shadow-md w-full max-w-md space-y-6">
            <h2 class="text-2xl font-bold text-center text-gray-800">用户登录</h2>
            <form @submit.prevent="handleLogin" class="space-y-4">
                <div>
                    <label for="username" class="block text-sm font-medium text-gray-700">用户名</label>
                    <input v-model="username" type="text" id="username"
                           class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
                    <span v-if="usernameError" class="text-red-500 text-sm">用户名不能为空</span>
                </div>
                <div>
                    <label for="password" class="block text-sm font-medium text-gray-700">密码</label>
                    <input v-model="password" type="password" id="password"
                           class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
                    <span v-if="passwordError" class="text-red-500 text-sm">密码不能为空</span>
                </div>
                <div>
                    <label for="captcha" class="block text-sm font-medium text-gray-700">验证码</label>
                    <div class="flex items-center">
                        <input v-model="captcha" type="text" id="captcha" class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
                        <img :src="captchaUrl" alt="验证码" @click="refreshCaptcha" class="ml-2 cursor-pointer">
                    </div>
                    <span v-if="captchaError" class="text-red-500 text-sm">请输入正确的验证码</span>
                </div>
                <div class="flex items-center justify-between">
                    <div class="flex items-center">
                        <input v-model="rememberMe" id="rememberMe" type="checkbox"
                               class="h-4 w-4 text-indigo-600 focus:ring-indigo-500 border-gray-300 rounded">
                        <label for="rememberMe" class="ml-2 block text-sm text-gray-900">记住我</label>
                    </div>
                    <a href="#" class="text-sm text-indigo-600 hover:text-indigo-900">忘记密码?</a>
                </div>
                <button type="submit"
                        class="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
                    登录
                </button>
            </form>
            <div class="text-center text-sm text-gray-600">
                还没有账号?
                <router-link to="/registration" class="text-indigo-600 hover:text-indigo-900">立即注册</router-link>
                <!--                <a href="#" class="text-indigo-600 hover:text-indigo-900">立即注册</a>-->
            </div>
        </div>
    </div>
</template>

<script setup>
import {ref} from 'vue';
import axios from 'axios';

const username = ref('');
const password = ref('');
const captcha = ref('');
const rememberMe = ref(false);
const usernameError = ref(false);
const passwordError = ref(false);
const loginError = ref('');

const fetchCaptcha = async () => {
    try {
        const response = await axios.get('/api/captcha');
        captchaUrl.value = response.data;
    } catch (error) {
        console.error('获取验证码出错:', error);
    }
};

const refreshCaptcha = () => {
    fetchCaptcha();
};

const handleLogin = async () => {
    usernameError.value = !username.value;
    passwordError.value = !password.value;

    if (!usernameError.value && !passwordError.value) {
        try {
            const response = await axios.post('/api/basic/login', {
                username: username.value,
                password: password.value,
                remember_me: rememberMe.value
            });
            if (response.data.success) {
                alert('登录成功!');
            } else {
            }
            loginError.value = response.data.message;
        } catch (error) {
            console.error(error);
            loginError.value = error.response.data.message;
            alert('登录失败!');
        }
    }
};
</script>

<style scoped>
body {
    background: linear-gradient(to bottom right, #f0f4f8, #d9e2ec);
}
</style>
